import com.fasterxml.jackson.databind.ObjectMapper;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

class R1{
    public  String users;
    public  String code;
}
class R2{
    public boolean OK;
}
@WebServlet("/PractiseJson")
public class Practise_Json extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
//        使用ObjectMapper从HttpServletRequest的输入流中读取JSON数据。
//        req.getInputStream()提供了请求体的原始字节流
//        readValue方法把JSON字符串转换成Java的Request对象。
        R1 r1 = objectMapper.readValue(req.getInputStream(), R1.class);
        System.out.println("username" +r1.users
               + "password"+r1.code);
        R2 r2 = new R2();
        r2.OK = true;
//        使用writeValueAsString方法将Response对象转换成JSON格式的字符串
        String s = objectMapper.writeValueAsString(r2);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(s);
    }
}
